package models

import (
	"crypto/md5"
	"encoding/hex"
	"github.com/go-playground/validator/v10"
	"gorm.io/gorm"
)

// User 用户模型
type User struct {
	gorm.Model
	Mobile   string `gorm:"type:varchar(64);column:mobile;default:'';comment:'手机号码'"`
	Email    string `gorm:"type:varchar(64);column:email;default:'';comment:'邮箱'" validate:"required,max=64,email"`
	Password string `gorm:"type:varchar(64);column:password;default:'';comment:'密码'"`
	Name     string `gorm:"type:varchar(64);column:name;default:'';comment:'名称'"`
	Address  string `gorm:"type:varchar(64);column:address;default:'';comment:'地址'"`
	Age      int    `gorm:"column:age;not null;comment:'年龄'"`
	//CreatedAt time.Time  `gorm:"column:created_at;autoCreateTime;comment:'创建时间'"`
	//UpdatedAt time.Time  `gorm:"column:updated_at;autoUpdateTime;comment:'修改时间'"`
	//DeletedAt *time.Time `gorm:"column:deleted_at;index;comment:'删除时间'"`
	Status int `gorm:"column:status;comment:'状态'"` // 假设状态只有0,1,2,3四种值
}

// Validate 验证用户数据
func (u *User) Validate() error {
	validate := validator.New()
	return validate.Struct(u)
}

type UpdateUserDTO struct {
	Mobile   string `json:"mobile" validate:"required,max=64"`
	Email    string `json:"email" validate:"required,max=64,email"`
	Password string `json:"password" validate:"required,max=64"`
	Name     string `json:"name" validate:"required,max=64"`
	Address  string `json:"address" validate:"max=64"`
	Age      int    `json:"age" validate:"required,min=0,max=150"`
	Status   int    `json:"status" validate:"oneof=1 2"`
}

type LoginUserDTO struct {
	Mobile   string `json:"mobile" validate:"required,max=64"`
	Password string `json:"password" validate:"required,max=64"`
}

// Validate 验证用户数据
func (dto *UpdateUserDTO) Validate() error {
	validate := validator.New()
	return validate.Struct(dto)
}

// TableName 指定表名
func (User) TableName() string {
	return "user"
}

// GenerateMD5 生成组合字符串的MD5哈希值
func GenerateMD5(password string) string {
	// 组合密码和另一个字符串
	combinedString := password + "golang"

	// 创建一个新的MD5哈希对象
	hash := md5.Sum([]byte(combinedString))

	// 将哈希结果转换为十六进制字符串
	return hex.EncodeToString(hash[:])
}
